package socket;

import javax.print.DocFlavor;
import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class TCPEchoClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            args = new String[]{"127.0.0.1", "0000000116121008422116360036862000000A6", "2000"};
        }
        if (args.length < 2 || (args.length > 3)) {
            throw new IllegalArgumentException(
                    "Parameter(s):<Server> <Word>[<Port>]");
        }

        String server = args[0];// server name or IP address  
        // Convert argument string to bytes using the default character encoding  
        String sendData = args[1];
        System.out.println("Data to sent:" + sendData);
        byte[] data = sendData.getBytes();
        int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;
        System.out.println("Server port:" + servPort);

        // create socket that is connected to server on specified port  
//        test1(server, data, servPort);
        try {
            //1.建立客户端socket连接，指定服务器位置及端口
            Socket socket = new Socket(server, servPort);
            //2.得到socket读写流
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            //输入流
            InputStream is = socket.getInputStream();
//            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            //3.利用流按照一定的操作，对socket进行读写操作
//            String info="用户名：Tom,用户密码：123456";
//            String info = "0000100001269032000005";
            while (true) {
//                String s = br.readLine();
                String s = "0000000116121008422116360036862000000A7";
                pw.print(s);
                pw.flush();
                if (s.equals("end")) {
                    break;
                }
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

//            socket.shutdownOutput();
            //接收服务器的相应
/*            String reply = null;
            while (!((reply = br.readLine()) == null)) {
                System.out.println("接收服务器的信息：" + reply);
            }*/
            //4.关闭资源
            br.close();
            is.close();
            pw.close();
            os.close();
            socket.close();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void test1(String server, byte[] data, int servPort) {
        Socket socket = null;
        try {
            socket = new Socket(server, servPort);

            System.out.println("Connected to server ... sending echo string");

            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            out.write(data);

            // receive
            int totalBytesRcvd = 0;// total bytes received so far
            int bytesRcvd;// Bytes received in last read

            while (totalBytesRcvd < data.length) {
                bytesRcvd = in.read(data, totalBytesRcvd, data.length
                        - totalBytesRcvd);
                if (bytesRcvd == -1) {
                    throw new SocketException("Connection closed prematurely");
                }
                totalBytesRcvd += bytesRcvd;
            }
            System.out.println("Received:" + new String(data));

        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}  